<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/signin.css" rel="stylesheet">
    <style>
        .col-2 {
            padding-top: 7px;
        }

        .form-check {
            padding-left: 7rem;
        }

        .form-group {
            margin-left: 5rem;
        }

        .align-middle {
            margin-left: 16rem;
            margin-bottom: 2rem;
        }

        .btn {
            margin-left: 10rem;
        }

        .comment {
            display: none;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="row align-items-center">
            <div class="col">

            </div>
            <div class="col">
                <div class="alert alert-danger alert-dismissible fade show invisible text-center" style="width: 42rem;"
                    role="alert">
                    <strong>提示：</strong><label for="" id="msg">555</label>
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
            </div>
            <div class="col">

            </div>

        </div>

        <div class="row align-items-center" style="height: 25rem;">
            <div class="col">

            </div>
            <div class="col">
                <div class="card shadow rounded" style="width: 42rem;">
                    <div class="card-body align-items-center">
                        <h2 class="align-middle">注册</h2>
                        <form id="registerForm" action="/registerDone" method="POST">

                            <div class="form-group row">
                                <label class="col-2" for="exampleInputEmail1">用户名：</label>
                                <input type="text" class="form-control col-6" id="username" name="username"
                                    aria-describedby="emailHelp">
                                <small id="comment" class="form-text text-muted comment">你的用户名或者密码错了，请确认后重试</small>
                            </div>
                            <div class="form-group row">
                                <label class="col-2" for="exampleInputPassword1">密码：</label>
                                <input type="password" class="form-control col-6" name="password" id="password">
                            </div>
                            <div class="form-group row">
                                <label class="col-2" for="exampleInputPassword1">确认密码：</label>
                                <input type="password" class="form-control col-6" name="confirmpassword"
                                    id="confirmpassword">
                            </div>

                            <button type="button" class="btn btn-primary" onclick="jump()">注册</button>
                            <button type="button" class="btn btn-primary">取消</button>
                        </form>
                    </div>
                </div>
            </div>
            <div class="col">

            </div>
        </div>

    </div>

    <script src="js/jquery-3.6.0.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script>
        // 点击注册时 执行的方法（获取数据，进行初步验证，验证通过则通过ajax向后台提交数据；否则提示验证不通过的信息）
        function jump() {
            let username = $('#username').val();
            let password = $('#password').val();
            let confirmpassword = $('#confirmpassword').val();
            if (username.length > 0 && password.length > 0 && password === confirmpassword) {
                // ajax请求
                $.ajax({
                    url: "/registerDone",
                    type: "POST",
                    data: $('#registerForm').serialize(),
                    dataType: "json",
                    success: function (res) {
                        console.log(res);
                        if(res.code===200){
                            window.location.href="/login";
                        }else{
                            alert_fn(res.msg);
                        }
                        
                    },
                    error: function (msg) {
                        alert_fn('网络或者服务器有问题');
                    }
                })
            } else {// 验证不通过，提示相关信息，并且阻止提交
                alert_fn();
                return false;
            }
        }

        // 这个方法包装了alert的相关行为和显示的内容
        function alert_fn(msg) {
            msg=msg || '密码不能为空，两次密码应该一致';
            // 替换提示的内容
            $('#msg').text(msg);
            // 移除不显示的class，则alert将显示
            $('.invisible').removeClass('invisible');
            // 三秒中后，给alert增加不显示的class，则alert重新隐藏
            setTimeout(() => {
                $('.alert').addClass('invisible');
            }, 3000);
        }
    </script>
</body>

</html>